package com.tianqu.tms.core.webservice.weichat.api.impl;

import java.rmi.RemoteException;

import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.enums.ApiLogFuncType;
import com.tianqu.tms.core.util.enums.WeChatResultCode;
import com.tianqu.tms.core.webservice.weichat.api.CarManager;
import com.tianqu.tms.core.webservice.weichat.api.UploadManager;

/**
 * @author Huang Wei
 *
 */
public class UploadManagerImpl  extends AbsoluteManager implements UploadManager{
    
	private static UploadManagerImpl INSTANCE = new UploadManagerImpl();
	
	private UploadManagerImpl(){};
	
	@Override
	public void uploadImageToMarketCar(String serial, String transferSerial, String imagePath) {
		try {
			StringBuffer log = new StringBuffer();
			log.append("UploadServiceImpl.uploadImageToMarketCar execute,");
			log.append(" serial :"+serial);
			log.append(" imagePath :"+imagePath);
			log.append(" transferSerial :"+transferSerial);
			log.append(" imagePath :"+imagePath);
			LOGGER.info(log);
			//
			String image = ImageUtils.transPicToStr(imagePath);
			int r = getUploadService().uploadImageToMarketCar(serial, transferSerial,image, image.length());
			if(WeChatResultCode.SUCCESS != WeChatResultCode.getResultCode(r)){
				LOGGER.error(log+" result:"+r);
				sendMobileMessage("failed to call UploadServiceImpl.uploadImageToMarketCar. serial:"+serial+","+WeChatResultCode.getResultCode(r).getMessage());
			} 
			//api log
			StringBuffer apiLog = new StringBuffer();
			apiLog.append("serial_"+serial).append("_");
			apiLog.append("transferSerial_"+transferSerial).append("_");
			apiLog.append("size_"+image.length()).append("_");
			apiLog.append("imagePath_"+imagePath);
			if(r!=WeChatResultCode.SUCCESS.getValue()) saveApiLog(ApiLogFuncType.MEMBER,apiLog.toString(),WeChatResultCode.getResultCode(r).getMessage());
		}catch (RemoteException e) {
			LOGGER.error("send message error.",e);
			sendMobileMessage("failed to call UploadServiceImpl.uploadImageToMarketCar. serial:"+serial+",服务请求超时");
		}catch (Exception e) {
			LOGGER.error("send message error.",e);
			sendMobileMessage("failed to call UploadServiceImpl.uploadImageToMarketCar. serial:"+serial+",客户端未知异常");
		}
	}
		
	public static UploadManagerImpl create() {
		return INSTANCE;
	}
}
